﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Permissions;
using System.Windows.Controls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebControls;
using PIT.Labs.ProjectRepository.Owl2DotNet;
using System.Linq;
using Microsoft.SharePoint.Administration;
using System.Threading;

namespace PIT.Labs.ProjectRepository.OntologyFeature
{
    /// <summary>
    /// <see cref="SPFieldText"/> to display and edit data properties of an ontology
    /// </summary>
    public class OntologyDataField : SPFieldText
    {
        private Guid ontologyGuid;

        /// <summary>
        /// Guid of the ontology file
        /// </summary>
        public Guid OntologyGuid
        {
            get
            {
                if (ontologyGuid == Guid.Empty)
                {
                    try
                    {
                        ontologyGuid = new Guid((string)this.GetCustomProperty("OntologyGuid"));
                    }
                    catch { 
                    //XXX: Log this!
                    }
                }
                return ontologyGuid;
            }
            set
            {
                ontologyGuid = value;
                this.SetCustomProperty("OntologyGuid", value.ToString());
            }
        }

        private Uri propertyUri;

        /// <summary>
        /// Uri of the data property in the ontology that is represented by the field
        /// </summary>
        public Uri PropertyUri
        {
            get
            {
                if (propertyUri == null)
                {
                    propertyUri = new Uri((string)this.GetCustomProperty("PropertyUri"));
                }
                return propertyUri;
            }
            set
            {
                propertyUri = value;
                this.SetCustomProperty("PropertyUri", value.ToString());
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="fieldName"></param>
        public OntologyDataField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            
        }

        /// <summary>
        /// Constrcutor
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="typeName"></param>
        /// <param name="displayName"></param>
        public OntologyDataField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
            
        }

        /// <summary>
        /// Get the control for rendering the field
        /// </summary>
        public override BaseFieldControl FieldRenderingControl
        {
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                var fieldControl = new OntologyDataFieldControl();

                return fieldControl;
            }
        }

        /// <summary>
        /// Validate the value of the field.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string GetValidatedString(object value)
        {
            if ((this.Required == true)
               &&
               String.IsNullOrEmpty(value as string))
            {
                throw new SPFieldValidationException(this.Title
                    + " must have a value.");
            }
            else
            {
                OntologyDataFieldValidationRule rule = new OntologyDataFieldValidationRule();
                ValidationResult result = rule.Validate(value, CultureInfo.InvariantCulture);

                if (!result.IsValid)
                {
                    throw new SPFieldValidationException((String)result.ErrorContent);
                }
                else
                {
                    return base.GetValidatedString(value);
                }
            }
        }
    }
}
